package com.sx.zongzhi.net;


import android.content.Intent;

import com.sx.zongzhi.MyApplication;
import com.sx.zongzhi.activity.LoginActivity;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by ZYG on 2017/11/17.
 * 回调的基类
 */

public abstract class BaseCallBack<T extends BaseResult> implements Callback<T> {

    @Override
    public void onResponse(Call<T> call, Response<T> response) {
        if (response.raw().code() != 200||response.body() == null) {
            onFailures("服务器异常！请稍后再试");
            return;
        }

        if (response.body().errcode == 20000) {
            onFailures(response.body().errmsg);
            Intent intent = new Intent(MyApplication.context, LoginActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            MyApplication.context.startActivity(intent);
            MyApplication.context.finishAllActivity();
            return;
        }
        if (response.body() != null){
            if (response.body().errcode!=0){
                onFailures(response.body().errmsg);
                return;
            }
            onSuccess(response.body());
        }else {
            onFailures("返回数据体为空");
        }
    }

    @Override
    public void onFailure(Call<T> call, Throwable t) {//网络问题会走该回调
        onFailures("检查网络，请稍后再试");
    }


    public abstract void onSuccess(T response);

    public abstract void onFailures(String message);
}
